﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using AutoMapper;
using Sharp.Infrastructure.Interfaces;

namespace Sharp.Infrastructure.AutoMapper
{
    public class AutoMapperInitialize : IAutoMapperInitialize
    {
        public void Initialize(params Assembly[] assemblys)
        {
            if (assemblys == null)
            {
                throw new NotSupportedException("不允许传入控制。");
            }

            //var filepath = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "");
            //var filepaths = Directory
            //    .GetFiles(Path.GetDirectoryName(filepath) ?? throw new InvalidOperationException(), "*.dll").ToList();


            Mapper.Initialize(cfg =>
            {
                foreach (var assembly in assemblys)
                {
                    foreach (var type in assembly.GetTypes())
                    {
                        if (type.IsAbstract || type.IsInterface) continue;

                        if (type.IsSubclassOf(typeof(Profile)))
                        {
                            cfg.AddProfile(assembly.CreateInstance(type.FullName) as Profile);
                        }
                    }
                }
            });

            //Mapper.AssertConfigurationIsValid();
        }
    }
}
